package com.example.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;


public class FileUploadUtils {
    private static final String UploadDir = "D:/第n次实验/public/picture"; // 请替换为实际的存储路径

    // 处理头像上传
    public static String processFileUpload(MultipartFile file) throws Exception {
        Path uploadPath = Paths.get(UploadDir);
        if (!Files.exists(uploadPath)) {
            Files.createDirectory(uploadPath);
        }
        // 获取文件名并构建目标路径
        String fileName = file.getOriginalFilename();
        Path filePath = uploadPath.resolve(fileName);

        // 将文件保存到指定路径
        Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);

        // 返回存储的文件路径或URL
        return filePath.toString();
    }

}

